<?php

class Bill_BillController extends Zend_Controller_Action
{

    public function init()
    {
        $contextSwitch = $this->_helper->getHelper('contextSwitch');
        $contextSwitch->addActionContext('edit', 'json')
		      ->addActionContext('saveposition', 'json')
		      ->addActionContext('list', 'json')
                      ->initContext();
    }

    public function indexAction()
    {
        // action body
    }

    public function editAction()
    {
        $this->_helper->layout()->disableLayout();
	Zend_Dojo::enableView($this->view);
	Zend_Dojo_View_Helper_Dojo::setUseDeclarative();
		
	$form = new Bill_Form_Bill();
	$modelBill = new Bill_Model_DbTable_Bill();
	
	$data = array();
	$data['created_on'] = time();
	
	$data['bill_id'] = $modelBill->insert($data);
	$form->populate($data);
	
	
	if($this->getRequest()->isPost())
	{
	    if($form->isValid($_POST))
	    {
		$data = $form->getValues();
		unset($data['bill_submit']);
		$data['updated_on'] = time();
		$modelBill = new Bill_Model_DbTable_Bill();
		
		if(is_numeric($data['bill_id']))
		{
		    echo $modelBill->update($data, 'bill_id = '. $data['bill_id']);
		    $modelBill->delete('updated_on = NULL OR updated_on = 0');
		}
		else
		{
		    $modelBill->insert($data);
		}
		
//		print_r($data);
	    }
	}
	else
	{
	    $this->view->form = $form;
	}
	
	
	
    }

    public function savepositionAction()
    {
	
	$modelPosition = new Bill_Model_DbTable_Position();
	
	if(is_numeric($_POST['position_id']))
	{
	    $this->view->message = "update";
	    $this->view->rows = $modelPosition->update($_POST, 'position_id = '.$_POST['position_id']);
	    
	} else
	{
	    $this->view->message = "insert";
//	    $message['myid'] = $modelPosition->insert($_POST);
	    $this->view->posid = $modelPosition->insert($_POST);
	}
    }

    public function listAction()
    {
        $this->_helper->viewRenderer->setNoRender(true);
	$model = new Bill_Model_DbTable_Bill();
	$items = $model->fetchAll()->toArray();
	$data = array();
	$this->view->identifier = 'bill_id';
	$data['items'] = array();
	foreach ($items as $item)
	{
	    $data['items'][] = $item;
	}
	$this->view->items=$data['items'];
    }


}







